/**
 * 
 */
package com.hawkfly.uploader;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import com.wbcs.jbsf.dao.IDataModal;
import com.wbcs.jbsf.dao.JBSFDaoTemplate;

/**
 * 支持数据库操作的上传
 * @author hawkfly
 */
public class Uploader4DB extends BaseUploader
{

    private String id;//资源插入到数据库后生成的资源编号
    
    public Uploader4DB(HttpServletRequest request)
    {
        super(request);
    }

    @Override
	public void upload() throws Exception {
        JBSFDaoTemplate daoTemplate = new JBSFDaoTemplate(true);
        int effectrows = 0;
        super.upload();
        if("SUCCESS".equals(super.getState())){//文件上传成功则更新数据库
            String selectid = "select SQU_SDCM.nextval id from dual";
            String insertsql = "insert into T_RESOURCE_INFO(ID, ZYLX, ZYBT, ZYJS, ZYLJ) values(?, 'A', ?, '', ?)";
            String rscid = daoTemplate.<String>select(selectid,new IDataModal<String>(){
                public String loadDatas(ResultSet rs,int ccount,ResultSetMetaData rmd,String dataModal) throws SQLException
                {
                    String rtnstr = null;
                    while(rs.next()){
                        rtnstr = rs.getString("id");
                    }
                    return rtnstr;
                }
             });
            effectrows = daoTemplate.update(insertsql,null, rscid, super.getTitle(), super.getUrl());
            if(effectrows != 0){//执行上传回滚操作，删除已上传文件并重置为失败状态
                id = rscid;
            }
        }
    }
    
    public String getId()
    {
        return id;
    }
    
}

